package registnet.base.source;

import grammar.PrerequisiteCruncher;

import java.util.List;
import java.util.ListIterator;

import registnet.base.CourseTaken;
import registnet.base.ITranscript;
import registnet.base.Prerequisite;
import registnet.base.Transcript;
import registnet.dao.Course;
import registnet.dao.CoursePeer;
import registnet.dao.Grade;
import registnet.dao.GradePeer;
import registnet.dao.Student;
import registnet.dao.StudentPeer;

/**
 * Defines a class for creating a <code>Transcript</code> from the data from a specific source.
 */
public class TranscriptSource implements ITranscriptSource {

	/**
	 * The transcript.
	 */
	private Transcript transcript;
	
	/**
	 * The student number of the student associated with the transcript.
	 */
	private String studentNumber;
	
	/**
	 * Creates a new transcript for a student with the given student number.
	 * @param studentNumber
	 */
	public TranscriptSource(String studentNumber){
		this.studentNumber = studentNumber;
		load();
	}
	
	@Override
	public ITranscript getTranscript() {
		return transcript;
	}

	@Override
	public ITranscript load() {
		
		if (transcript != null) return transcript;
		else transcript = new Transcript();
		
		try{
			
			
			//studentNumber
			Student daoStudent = new StudentPeer().getStudent(studentNumber);
			transcript.setStudentNumber(studentNumber);
			
			//courses
			List<Grade> daoGrades = new GradePeer().getGradeByStudent(daoStudent.getStudentId());
			for(int i=0; i<daoGrades.size(); i++){
				CourseTaken course = new CourseTaken();
				Grade daoGrade = daoGrades.get(i);
				
				if(!daoGrade.getSectionName().equals("REMOVAL") && !daoGrade.getSectionName().equals("COMPLETION")){
					
					//PE Courses
					if(daoGrade.getCourse().getCourseCode().equals("PE 2")){
						String[] tmp = daoGrade.getSectionName().split("-");
						course.setCourseCode(daoGrade.getCourse().getCourseCode()+"-"+tmp[0]);
						course.setCourseName(new CoursePeer().getCourse(course.getCourseCode()).getCourseName());
					}
					//courses which can differ in credit/units
					else if(daoGrade.getSectionName().contains("(")){
						String tmp = Character.toString(daoGrade.getSectionName().charAt(daoGrade.getSectionName().indexOf("(")+1));
						course.setCourseCode(daoGrade.getCourse().getCourseCode()+"-"+tmp);
						course.setCourseName(new CoursePeer().getCourse(course.getCourseCode()).getCourseName());
					}
					//default courses
					else{
						course.setCourseCode(daoGrade.getCourse().getCourseCode());
						course.setCourseName(daoGrade.getCourse().getCourseName());
					}
					
					//prerequisites
					Course daoCourse = new CoursePeer().getCourse(daoGrade.getCourse().getCourseCode());
					List<String> prerequisiteList = PrerequisiteCruncher.getPrerequisites(daoCourse.getPrerequisite());
					ListIterator<String> temp = prerequisiteList.listIterator();
					while(temp.hasNext()){
						Prerequisite prerequisite = new Prerequisite(temp.next());
						course.addPrerequisite(prerequisite);
					}
					
					//unit
					course.setUnit(new CoursePeer().getCourse(course.getCourseCode()).getUnits());
					
					//semester
					
					//year
					
					//semesterTaken
					course.setSemesterTaken(daoGrade.getSemester());
					
					//yearTaken
					course.setYearTaken(daoGrade.getAcademicYear());
					
					//grade
					course.setGrade(daoGrade.getGrade());
					
					//sectionName
					course.setSectionName(daoGrade.getSectionName());
					
					//remarks
					course.setRemarks(daoGrade.getRemarks());
					
					//credit
					course.setCredit(daoGrade.getCredit());
					
					//reExam
					course.setReExam(0);
					
					transcript.addCourse(course);
				}else{
					transcript.updateReexamCourse(daoGrade.getCourse().getCourseCode(), daoGrade.getGrade());
				}
				
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return transcript;
	}

}
